/* xxalpnum.i-NUMBER RANGE MANAGEMENT ENGINE-ALPHBETIC-NUMERICS TYPE SEGMENTS */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.11 $                                                          */
/*V8:ConvertMode=Maintenance                                                  */
/* REVISION: 8.6      LAST MODIFIED: 04/30/96   BY: PCD *K002*                */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/*                                   09/27/96   BY: jzw *K00R*                */
/*                                   10/22/96   BY: jzw *K018*                */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane   */
/* REVISION: 9.1      LAST MODIFIED: 08/13/00   BY: *N0KR* Mark Brown         */
/* $Revision: 1.11 $    BY: Jean Miller           DATE: 04/08/02  ECO: *P058*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* Revision: 9.1    Initial Release: 28/10/03   BY: *cli* cli   SPEC: HK-S-014*/
/* This program is modified from nrfiscal.i - FISCAL-TYPE SEGMENTS, However,  */
/* most of the code have to re-write. Therefore it becomes an Inital Release. */ 
/******************************************************************************/



/* ******************************************************************** */

PROCEDURE seg_make_alpnum:
   /* Create a new segment data structure of the FISCAL-PERIOD type. */
   define output parameter new_segment as recid no-undo.

   define variable dummy as logical no-undo.

   /* Only one date-driven segment allowed */
   for each segment where seg_attached and
      (seg_rank = "1" or seg_rank = "0"):
      error_state = true.
      error_nbr   = 2922. /* Seq already contains a date-driven seg */
      return.
   end.

   create segment.
   new_segment = recid (segment).

   assign
      seg_type          = "5"  
      seg_exe_generate  = "seg_generate_alpnum"
      seg_exe_validate  = "seg_validate_alpnum"
      seg_exe_edit      = "seg_edit_alpnum"
      seg_exe_display   = "seg_display_alpnum"
      seg_format        = "Y"
      seg_rank          = "1"
      seg_valuemask     = fill("09",4).

END PROCEDURE.

PROCEDURE seg_set_alpnum:
   define input parameter p-seg-recid as recid no-undo.
   define input parameter alpnum-format as character no-undo.
   define input parameter control-seg as logical no-undo.

   define variable dummy        as logical no-undo.
   define variable reset        as logical initial false no-undo.
   define variable format-valid as logical initial true no-undo.
   define variable year-mask    as character no-undo.
   define variable value-mask   as character no-undo.
   define variable date-min     as character no-undo.
   define variable date-max     as character no-undo.
   define variable hold-date    as date no-undo.
   define variable i            as integer initial 1 no-undo.

   /* Segment format valid */
   find segment where recid(segment) = p-seg-recid no-error.
   assign seg_format = alpnum-format.

   /* Calculate the valuemask for the fiscal segment */
   hold-date = trans_effdate.
   trans_effdate = effdate.
   run seg_format_alpnum
      (p-seg-recid,
       output date-min ).
   if error_state then return.

   trans_effdate = expdate.
   run seg_format_alpnum
      (p-seg-recid, output date-max ).
   if error_state then return.

   trans_effdate = hold-date.

   do while not reset and i <= 4:
      year-mask = year-mask + substring(string(year(effdate)),i,1)
                            + substring(string(year(expdate)),i,1).
      if substring(string(year(effdate)),i,1) <
         substring(string(year(expdate)),i,1) then reset = true.
      i = i + 1.
   end.

   if i <> 4 then year-mask = year-mask + fill("09", 5 - i ).

   /* Create valuemask for segment */
   do i = 1 to length(alpnum-format):

      if substring(alpnum-format,i,1) = "Y" then do:
         value-mask = value-mask + year-mask.
         date-min = substring(date-min,5).
         date-max = substring(date-max,5).
      end.
      else do:
         value-mask = value-mask + fill( substring(alpnum-format,i,1),2).
         date-min = substring(date-min,2).
         date-max = substring(date-max,2).
      end.
   end.

   assign seg_valuemask = value-mask.

   if control-seg then assign seg_rank = "0".
   else assign seg_rank = "1".

   run seg_generate_alpnum
      (input p-seg-recid,
       output dummy).

END PROCEDURE.

PROCEDURE seg_generate_alpnum:
   define input parameter p-seg-recid as recid no-undo.
   define output parameter reset as logical initial false no-undo.

   define variable seg-fmt-val as character no-undo.

   run seg_format_alpnum
      (input p-seg-recid,
       output seg-fmt-val ).
   if error_state then return.

   find segment where recid(segment) = p-seg-recid no-error.

   if seg-fmt-val <> seg_value
   then do:
      assign
         seg_value = seg-fmt-val.
      reset = true.
   end.

END PROCEDURE.

PROCEDURE seg_validate_alpnum:
   define input parameter p-seg-recid as recid no-undo.
   define input-output parameter val as character no-undo.
   define output parameter is-valid as logical initial true no-undo.

   define variable seg-fmt-val as character no-undo.

   run seg_format_alpnum ( p-seg-recid, output seg-fmt-val ).

   if seg-fmt-val <> substring(val,1,length(seg-fmt-val)) then
      is-valid = false.
   else
      val = substring(val, length(seg-fmt-val) + 1 ).

END PROCEDURE.

PROCEDURE seg_format_alpnum:
   define input parameter p-seg-recid as recid no-undo.
   define output parameter seg-fmt-val as character no-undo.
   define variable val as character no-undo.
   define variable segval as character no-undo.
   define variable k as integer no-undo.
   define variable src as character init "".
   define variable pos as INTEGER init 0.  
   define variable delim as CHAR INIT ",".
   define variable is_match as logical init no.


   define variable i as integer no-undo.
   find segment where recid(segment) = p-seg-recid no-error.

   do i = 1 to length(segment.seg_format):
      CASE substring(segment.seg_format,i,1):
        
         when "Y" then do:
            if integer(substring(string(YEAR(TODAY)),3,2)) < 10 then do:
                seg-fmt-val = seg-fmt-val + substring(string(YEAR(TODAY)),4,1).
                is_match = yes.
            end.
            else do:
                seg-fmt-val = seg-fmt-val + substring(string(YEAR(TODAY)),3,2).
                assign src = seg_alpnum.
                do  k = 1 to 36:
                    pos = index (src, delim).
                    val = substring (src, 1, pos - 1).
                    src = substring (src, pos + 1).
                    if substring(val,1,2) = seg-fmt-val then do:
                       seg-fmt-val = substring(val,3,1).
                       is_match = yes.
                      leave.
                    end.
                    pos = pos + 1.
                end.
            end.
            if is_match = no then 
            seg-fmt-val = "0".
            
         end.

      END CASE.
   end. /* DO */

END PROCEDURE.

PROCEDURE seg_edit_alpnum:

   define input parameter seg-recno as recid.

   define variable new-seg-nbr like seg_nbr format ">9" label "New Seg Nbr".
   define variable alpnum-format as character init "Y".
   define variable control-seg as logical format "yes/no"
      label "Control Segment".

   FORM /*GUI*/ 
      
 RECT-FRAME       AT ROW 1.4 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1   COLUMN 3 NO-LABEL
 SKIP(.1)  /*GUI*/
      new-seg-nbr    colon 12
      control-seg    colon 45
    SKIP(.4)  /*GUI*/
with side-labels width 80
   
   with frame segalpnum overlay centered NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-segalpnum-title AS CHARACTER.
 F-segalpnum-title = "Year (Alphbetic Numeric) Editor".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame segalpnum = F-segalpnum-title.
 RECT-FRAME-LABEL:WIDTH-PIXELS in frame segalpnum =
  FONT-TABLE:GET-TEXT-WIDTH-PIXELS(
  RECT-FRAME-LABEL:SCREEN-VALUE in frame segalpnum + " ", RECT-FRAME-LABEL:FONT).
 RECT-FRAME:HEIGHT-PIXELS in frame segalpnum =
  FRAME segalpnum:HEIGHT-PIXELS - RECT-FRAME:Y in frame segalpnum - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME segalpnum = FRAME segalpnum:WIDTH-CHARS - .5. /*GUI*/


   /* SET EXTERNAL LABELS */
   setFrameLabels(frame segalpnum:handle).

   ststatus = stline[2].
   status input ststatus.

   find segment where recid (segment) = seg-recno.
   if seg_rank = "0" then control-seg = true.

   display
      seg_nbr     @ new-seg-nbr
      control-seg
   with frame segalpnum.

   segment_edited = false.

   set_block:
   do on endkey undo, leave:

      run nr_clear_error.

      set
         new-seg-nbr
         control-seg
      go-on (F5 CTRL-D)
      with frame segalpnum editing:

         /* REJECT ENTRY OF UNKNOWN */
         {gprejqm.i}

         /* Delete logic. */
         {nrdel.i
            &seg-recno = seg-recno
            &undo-block = set_block
            &frame = segalpnum}

         if del-yn then do:
            hide frame segalpnum.
            return.
         end.
      end.

      if new-seg-nbr <> seg_nbr and
         (new-seg-nbr > segment_count or new-seg-nbr < 1 )
      then do:
         {pxmsg.i &MSGNUM=2932 &ERRORLEVEL=3}
         /* Segment numbers must be consecutive */
         next-prompt new-seg-nbr with frame segalpnum.
         undo set_block.
      end.

      run seg_set_alpnum
         (input seg-recno,
          input alpnum-format,
          input control-seg).


      if error_state then do:
         {pxmsg.i &MSGNUM=error_nbr &ERRORLEVEL=3}
         next-prompt alpnum-format with frame segalpnum.
         undo set_block, retry.
      end.

      if new-seg-nbr <> seg_nbr then
         run nr_set_seg_nbr
            (input seg_nbr,
             input new-seg-nbr ).

      segment_edited = true.

   end.

   hide frame segalpnum.

END PROCEDURE.

PROCEDURE seg_display_alpnum:
   define input parameter seg-recno as recid no-undo.
   define output parameter seg-settings as character no-undo.
   define output parameter seg-control as character no-undo.

   define variable i as integer no-undo.

   find segment where recid(segment) = seg-recno.

   seg-settings = seg_format + " (".

   do i = 1 to length(seg_format):
      if substring(seg_format,i,1) = "Y" then
         seg-settings = seg-settings + "Y". 

      else
         seg-settings = seg-settings + substring(seg_format,i,1).
   end.

   seg-settings = seg-settings + ")".
   seg-control = string(seg_rank = "0").

END PROCEDURE.
